package com.concerneyes.glasses.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.concerneyes.R;
import com.concerneyes.glasses.model.GlassesDetailModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/19 0019.
 */

public class GlassesDetailAdapter extends BaseAdapter {
    private Context context;
    private List<GlassesDetailModel.RefractiveListBean> data = new ArrayList<>();
    private int selectedPosition = 0;

    public GlassesDetailAdapter(Context context) {
        this.context = context;
    }

    public void clearList() {
        data.clear();
        notifyDataSetChanged();

    }

    public void addDatas(List<GlassesDetailModel.RefractiveListBean> datas) {
        if (datas != null) {
            this.data.addAll(datas);
        }
        notifyDataSetChanged();
    }

    public void setSelection(int position) {
        selectedPosition = position;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public GlassesDetailModel.RefractiveListBean getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_glasses_datail, null);
            holder.glassesDetail = (TextView) convertView.findViewById(R.id.tv_glasses_detail);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String title = data.get(position).getTitle();
        holder.glassesDetail.setText(title);
        if (selectedPosition == position) {
            holder.glassesDetail.setTextColor(context.getResources().getColor(R.color.white));
            holder.glassesDetail.setBackgroundResource(R.drawable.btn_blue_bg);
        } else {
            holder.glassesDetail.setTextColor(context.getResources().getColor(R.color.black_three));
            holder.glassesDetail.setBackgroundResource(R.drawable.btn_white_bg);
        }
        return convertView;
    }

    class ViewHolder {
        TextView glassesDetail;
    }
}
